package chain;

// 抽象处理者：定义请假审批的接口
abstract class LeaveHandler {
    protected LeaveHandler nextHandler; // 指向下一个处理者

    public void setNextHandler(LeaveHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    // 处理请假的抽象方法
    public abstract void handleRequest(int leaveDays);
}

// 具体处理者：经理（可审批1-3天）
class Manager extends LeaveHandler {
    @Override
    public void handleRequest(int leaveDays) {
        if (leaveDays <= 3) {
            System.out.println("经理批准了 " + leaveDays + " 天的请假");
        } else if (nextHandler != null) {
            nextHandler.handleRequest(leaveDays);
        }
    }
}

// 具体处理者：部门经理（可审批4-7天）
class Director extends LeaveHandler {
    @Override
    public void handleRequest(int leaveDays) {
        if (leaveDays <= 7) {
            System.out.println("部门经理批准了 " + leaveDays + " 天的请假");
        } else if (nextHandler != null) {
            nextHandler.handleRequest(leaveDays);
        }
    }
}

// 具体处理者：总经理（可审批超过7天）
class CEO extends LeaveHandler {
    @Override
    public void handleRequest(int leaveDays) {
        if (leaveDays > 7) {
            System.out.println("总经理批准了 " + leaveDays + " 天的请假");
        }
    }
}

public class ChainOfResponsibilityTest {
    public static void main(String[] args) {
        // 创建处理者对象
        LeaveHandler manager = new Manager();
        LeaveHandler director = new Director();
        LeaveHandler ceo = new CEO();

        // 组装责任链
        manager.setNextHandler(director);
        director.setNextHandler(ceo);

        manager.handleRequest(2);   //经理批准了 2 天的请假
        manager.handleRequest(5);   //部门经理批准了 5 天的请假
        manager.handleRequest(10);  //总经理批准了 10 天的请假
    }
}